//
//  AppDelegate.swift
//  Weibo
//
//  Created by mac on 15/12/12.
//  Copyright © 2015年 MK. All rights reserved.
//

/*
day03 - task: 页面之间的跳转
1. 用户未登录 -> OAuth登录页面 -> 欢迎页面 -> 首页
2. 用户已登录
2.1 版本未更新 -> 欢迎页面 -> 首页
2.2 版本已更新 -> 新特性页面 -> 首页

> 利用偏好设置保存当前版本  然后取出当前版本与偏好设置中的版本做比较 一样->欢迎页  不一样->新特性
> 利用通知 一对多 让根视图＝首页
*/

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        //调用解档数据
        //let gotData = MKUserInfo.gotUserInfoData()
        //print(gotData)
        
        //主窗口
        window = UIWindow()
        window?.rootViewController = changeRootController()
        //- 欢迎页面  //MKWelcomeController()
        //- 新特性页面 //MKNewFeatureController()
        //- 访客页面   //MainViewController()
        window?.backgroundColor = UIColor.whiteColor()
        window?.makeKeyAndVisible()
        
        //调用通知
        regiesterNotifiaction()
        
        //更改系统默认的主题颜色
        setThemeColor()
        
        return true
    }
    
    //FIXME: -1首先判断是否登录 2利用偏好设置 判断版本 设置根控制器=欢迎页?新特性 3利用通知 1对多 跳转到首页
    private func changeRootController() ->UIViewController {
        //是否登录 -登录就判断版本 -未登录就设置成访客视图
        if MKUserInfoViewModer().userLogin {
            
            //是否为新版本 -新版本跳转到新特性 老版本跳转到首页
            return isNewservison() ? MKNewFeatureController() : MKWelcomeController()
        }
        
        return MainViewController()
    }
    
    //FIXME: -判断版本
    private func isNewservison() ->Bool {
        
        //获取当前版本
        let info = NSBundle.mainBundle().infoDictionary!
        let currentVersion = info["CFBundleShortVersionString"] as! String
        let currentNum = Double(currentVersion)!
        //print(currentNum)
        
        //获取沙盒中的版本 --取出的是上一次存储的版本
        let userDefaults = NSUserDefaults.standardUserDefaults()
        let key = "sandBox"
        let boxNum = userDefaults.doubleForKey(key)
        //print(boxNum)
        //立即写入到偏好设置 --写入的是当前版本
        userDefaults.setDouble(currentNum, forKey: key)
        userDefaults.synchronize()
        
        //比较两个版本 -- 如果是新版本返回true  如果是老版本返回false
        return currentNum > boxNum
    }
    
    //FIXME: -利用 通知 进行更改根控制器=首页  -具体实现:1注册通知 2移除通知 3接收到通知去修改根控制器 4发送通知
    private func regiesterNotifiaction() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "notiChangeRootController:", name:notificationName , object: nil)
    }
    
    //移除通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    //接收通知 修改根控制器 -弄个参数的原因是 欢迎页->首页 新特性->首页  登录页->欢迎页(->首页)
    @objc private func notiChangeRootController(notif: NSNotification) {
        //参数 是通知者 - 如果指定的通知者 不为空->欢迎页  为空->首页
        //print(notif)
        
        if notif.object != nil {
            window?.rootViewController = MKWelcomeController()
            return
        }
        
        window?.rootViewController = MainViewController()/*MKHomeTableViewController()*/
    }
    
    
    //更改系统默认的主题颜色
    func setThemeColor() {
        UITabBar.appearance().tintColor = themeColor
        UINavigationBar.appearance().tintColor = themeColor
    }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    
    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }
    
    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }
    
    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
    
    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    
}

